package com.wuyou.service.impl;

import com.wuyou.domain.Result;
import com.wuyou.entity.User;
import com.wuyou.mapper.UserMapper;
import com.wuyou.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuyou.service.bo.UserBO;
import com.wuyou.util.ShiroUtils;
import com.wuyou.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘棋城
 * @since 2022-09-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getUser() {
        List<User> users = userMapper.selectList(null);
        return users;
    }

    @Override
    public User getUserById(Long userId) {
        User user = userMapper.selectById(userId);
        return user;
    }

    @Override
    public int userAdd(UserBO bo) {
        //生成盐
        String salt = ShiroUtils.generateSalt(6);
        //密码加密
        String password = ShiroUtils.encryptPassword("MD5", bo.getPassword(), salt, 3);
        //加盐
        bo.setSalt(salt);
        //密码
        bo.setPassword(password);
        //创建时间
        bo.setCreatTime(new Date());
        //状态
        bo.setStatus(0);
        //身份(是否房东0)
        bo.setIsHost(0);
        //bopo
        User user = User.builder().build();
        BeanUtils.copyProperties(bo, user);

        userMapper.insert(user);
        return 0;
    }
}
